from flask import Blueprint, render_template, request
from applications.common.utils.rights import authorize
from applications.common.utils.http import table_api
from applications.common.curd import model_to_dicts
from applications.common.helper import ModelFilter
from applications.common.utils.validate import xss_escape
from applications.models.admin_credible import Credible_Fire, Credible_Earthquake, Credible_Run, Credible_Fight
from applications.schemas.admin_credible import Credible_FireOutSchema, Credible_EarthquakeOutSchema, \
    Credible_RunOutSchema, Credible_FightOutSchema

admin_credible = Blueprint('adminCredible', __name__, url_prefix='/admin/credible')

@admin_credible.get('/')
@authorize("credible:info", log=True)
def credible_info():

    return render_template('admin/credible/credible.html')

#   用户分页查询
@admin_credible.get('/credible_fire')
# @authorize("admin:fire:main", log=True)
def credible_fire():

    # 获取请求参数
    id = request.args.get('id', type=int)
    Name = xss_escape(request.args.get('Name', type=str))
    Occur = xss_escape(request.args.get('Occur', type=str))
    Method = xss_escape(request.args.get('Method', type=str))
    Score = xss_escape(request.args.get('Score', type=str))

    # 查询参数构造
    mf = ModelFilter()
    if id:
        mf.exact(field_name="id", value=id)
    if Name:
        mf.contains(field_name="Name", value=Name)
    if Occur:
        mf.contains(field_name="Occur", value=Occur)
    if Method:
        mf.contains(field_name="Method", value=Method)
    if Score:
        mf.contains(field_name="Score", value=Score)


    # orm查询
    # 使用分页获取data需要.items
    credible_fire = Credible_Fire.query.filter(mf.get_filter(model=Credible_Fire)).layui_paginate()
    count = credible_fire.total
    # 返回api
    return table_api(data=model_to_dicts(schema=Credible_FireOutSchema, data=credible_fire.items), count=count)

#   用户分页查询
@admin_credible.get('/credible_earthquake')
# @authorize("admin:fire:main", log=True)
def credible_earthquake():

    # 获取请求参数
    id = request.args.get('id', type=int)
    Name = xss_escape(request.args.get('Name', type=str))
    Occur = xss_escape(request.args.get('Occur', type=str))
    Method = xss_escape(request.args.get('Method', type=str))
    Score = xss_escape(request.args.get('Score', type=str))

    # 查询参数构造
    mf = ModelFilter()
    if id:
        mf.exact(field_name="id", value=id)
    if Name:
        mf.contains(field_name="Name", value=Name)
    if Occur:
        mf.contains(field_name="Ring", value=Occur)
    if Method:
        mf.contains(field_name="Ring", value=Method)
    if Score:
        mf.contains(field_name="Ring", value=Score)


    # orm查询
    # 使用分页获取data需要.items
    credible_earthquake = Credible_Earthquake.query.filter(mf.get_filter(model=Credible_Earthquake)).layui_paginate()
    count = credible_earthquake.total
    # 返回api
    return table_api(data=model_to_dicts(schema=Credible_EarthquakeOutSchema, data=credible_earthquake.items), count=count)


#   用户分页查询
@admin_credible.get('/credible_run')
# @authorize("admin:fire:main", log=True)
def credible_run():

    # 获取请求参数
    id = request.args.get('id', type=int)
    Name = xss_escape(request.args.get('Name', type=str))
    Occur = xss_escape(request.args.get('Occur', type=str))
    Method = xss_escape(request.args.get('Method', type=str))
    Score = xss_escape(request.args.get('Score', type=str))

    # 查询参数构造
    mf = ModelFilter()
    if id:
        mf.exact(field_name="id", value=id)
    if Name:
        mf.contains(field_name="Name", value=Name)
    if Occur:
        mf.contains(field_name="Occur", value=Occur)
    if Method:
        mf.contains(field_name="Method", value=Method)
    if Score:
        mf.contains(field_name="Score", value=Score)

    # orm查询
    # 使用分页获取data需要.items
    credible_run = Credible_Run.query.filter(mf.get_filter(model=Credible_Run)).layui_paginate()
    count = credible_run.total
    # 返回api
    return table_api(data=model_to_dicts(schema=Credible_RunOutSchema, data=credible_run.items), count=count)


#   用户分页查询
@admin_credible.get('/credible_fight')
# @authorize("admin:fire:main", log=True)
def credible_fight():

    # 获取请求参数
    id = request.args.get('id', type=int)
    Name = xss_escape(request.args.get('Name', type=str))
    Occur = xss_escape(request.args.get('Occur', type=str))
    Method = xss_escape(request.args.get('Method', type=str))
    Score = xss_escape(request.args.get('Score', type=str))

    # 查询参数构造
    mf = ModelFilter()
    if id:
        mf.exact(field_name="id", value=id)
    if Name:
        mf.contains(field_name="Name", value=Name)
    if Occur:
        mf.contains(field_name="Occur", value=Occur)
    if Method:
        mf.contains(field_name="Method", value=Method)
    if Score:
        mf.contains(field_name="Score", value=Score)

    # orm查询
    # 使用分页获取data需要.items
    credible_fight = Credible_Fight.query.filter(mf.get_filter(model=Credible_Fight)).layui_paginate()
    count = credible_fight.total
    # 返回api
    return table_api(data=model_to_dicts(schema=Credible_FightOutSchema, data=credible_fight.items), count=count)

